Repository navigation
Keep the GC root set alive until the process ends - #1465
Conversation
hx::thread::Thread_obj::current() gives every thread that it is called on, including foreign threads, a thread-local ThreadInfoHolder whose destructor calls hx::GCRemoveRoot. Threads that are still running when the process exits, such as the libuv thread pool, which libuv only joins from a destructor function, run that destructor after the static objects have been destroyed. GCRemoveRoot then erased from the destroyed static root set, which corrupted the heap or crashed the process after main had returned. Allocate the root set on first use and never free it, like the offset root set next to it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDtgRomWVWe99UmFhn
|
Correction: HaxeFoundation/haxe#12897 was already reverted by HaxeFoundation/haxe#13143. However, the nightly that build.haxe.org serves as The failure is a compile error from Generated by Claude Code |
|
HaxeFoundation/haxe#12897 was reverted by HaxeFoundation/haxe#13143, so this shouldn't be happening anymore. Also, what's the reason for not always initializing |
The root lock is only created in InitAlloc, so no root can be added before the set exists, and the null checks are unnecessary. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDtgRomWVWe99UmFhn
Right. Those jobs built with Haxe 886a9c7, which is older than the revert (badcb0a), so they used a nightly from before it landed. A run with a current nightly shouldn't hit this. I've corrected my earlier comment.
There isn't a good one. Generated by Claude Code |
|
I'm not much of a C++ person but this makes sense to me and looks safe. While we're here, did you check Immix for other occurrences of this pattern? |
Yes. Code can reach GC state after the static destructors have run in two ways: 1. Thread-local destructors. 2. A Haxe thread that finishes its job during exit and calls
The remaining So the root set is the only instance reachable from an idle thread. The debugger's Generated by Claude Code |
|
I'll merge this because the hxcoro CI is ruining my day right now, and that way I can see if it actually helps and solves the crashes I've been seeing. |
Problem
hx::thread::Thread_obj::current()gives every thread it is called on, foreign threads included, athread_local ThreadInfoHolder(src/hx/thread/ThreadImpl.cpp) whose destructor callshx::GCRemoveRoot. That erases fromsgRootSet, astatic std::unordered_setinImmix.cpp.Threads that are still alive when the process exits run that destructor after the static objects have been destroyed. libuv's thread pool is one: libuv only joins its workers from
uv_library_shutdown, a__attribute__((destructor))function that runs from_dl_fini, afterexit()has run the static destructors. The workers then erase from the destroyed set. That is undefined behaviour, and it shows up as different symptoms.This is the intermittent crash after all tests had passed in hxcoro's cpp CI (HaxeFoundation/hxcoro#219):
double free or corruption (!prev),corrupted size vs. prev_size while consolidating, or, as in this core dump from a CI run that looped the test binary, a SIGSEGV thatHXCPP_CATCH_SEGVturns intostd::terminate:Locally it doesn't crash, but gdb shows the same order: the pool threads call
GCRemoveRootafter the set's destructor has run. Its element count reads 0, and its bucket pointer is stale.Change
Make
sgRootSeta pointer to a set that is allocated in its static initializer and never freed.GCAddRootalready requiressGCRootLock, whichInitAlloccreates, so no root can be added before the set exists, and the code needs no null checks.sGCRootLockis itself a pointer that is never freed, so the lock taken inGCRemoveRootstays valid as well.The comments below cover the other places in Immix and the debugger that could be reached after static destruction.
Testing
exit(), all four libuv pool threads now remove their roots from the live, heap-allocated set (same pointer, counts 6, 6, 6, 5) instead of freed memory.atexitworkaround that will not be merged. This PR fixes the cause instead.(latest) / runtime testsfailures come from a Haxe nightly that predates the revert of Fix unreflective haxe#12897 (see the comments below), not from this change.🤖 Generated with Claude Code
https://claude.ai/code/session_01Va4FXDtgRomWVWe99UmFhn